Type.registerNamespace('ScottCate');

ScottCate.Automobile = function(color) {
    this._color = color;
    this._maxSpeed = 0;
    this._numberOfWheels = 4;
}
ScottCate.Automobile.prototype = {
    get_color: function() { return this._color; },
    get_maxSpeed: function() {return this._maxSpeed; },
    get_wheelCount: function() { return this._numberOfWheels; }
}

ScottCate.Automobile.registerClass('ScottCate.Automobile', null, Sys.IDisposable);

//Define and register Cheap Car
ScottCate.CheapCar = function(color) {
    ScottCate.CheapCar.initializeBase(this, [color]);
    this._maxSpeed = 55;
}
ScottCate.CheapCar.registerClass('ScottCate.CheapCar', ScottCate.Automobile);

//Define and register Corvette
ScottCate.Corvette = function(color) {
    ScottCate.Corvette.initializeBase(this, [color]);
    this._maxSpeed = 155;
}
ScottCate.Corvette.registerClass('ScottCate.Corvette', ScottCate.Automobile);

//Define and register Ferrari
ScottCate.Ferrari = function(color) {
    ScottCate.Ferrari.initializeBase(this, [color]);
    this._maxSpeed = 255;
}
ScottCate.Ferrari.registerClass('ScottCate.Ferrari', ScottCate.Automobile);

//Define and register Dump Truck
ScottCate.Dumptruck = function(color) {
    ScottCate.Dumptruck.initializeBase(this, [color]);
    this._maxSpeed = 15;
}
ScottCate.Dumptruck.registerClass('ScottCate.Dumptruck', ScottCate.Automobile);

//Define and register Motocycle
ScottCate.Motorcycle = function(color) {
    ScottCate.Motorcycle.initializeBase(this, [color]);
    this._maxSpeed = 65;
    this._numberOfWheels = 2;
}
ScottCate.Motorcycle.registerClass('ScottCate.Motorcycle', ScottCate.Automobile);

//Define and register Speed Bike
ScottCate.SpeedBike = function(color) {
    ScottCate.SpeedBike.initializeBase(this, [color]);
    this._maxSpeed = 233;
}
ScottCate.SpeedBike.registerClass('ScottCate.SpeedBike', ScottCate.Motorcycle);







// Example 2
// Show no encapsulation possible programming errors
//Define and register Sports Car
ScottCate.SportsCar = function(speed) {
    ScottCate.SportsCar.initializeBase(this, [speed]);
    this.set_maxSpeed(speed);
}

ScottCate.SportsCar.prototype = {
    set_maxSpeed: function(speed) {
        if(speed < 150) {
            var message = "Sports Cars have to be able to reach 150 MPH";
            var err = Error.argumentOutOfRange('speed', speed, message);
            throw err;
        }
        else {
            this._maxSpeed = speed;
        }
    }
}
ScottCate.SportsCar.registerClass('ScottCate.SportsCar', ScottCate.Automobile);

function CreateFastSportsCar () {
    var reallyFastCar = new ScottCate.SportsCar('250');
    WriteMaxSpeed(reallyFastCar);
}

function ShowSportsCarSpeedError() {
    var crapCar = new ScottCate.SportsCar('50');    
    WriteMaxSpeed(reallyFastCar);
}

function ProveNoEncapsulation() {
    var slow = new ScottCate.SportsCar();
    slow._maxSpeed = 50;
    slow._color = "Slow Color";
    WriteMaxSpeed(slow);
}



//helper methods

function WriteMaxSpeed(auto) {
    var sb = new Sys.StringBuilder('<div>');
    WriteLineToStringBuilder(sb, 'Object Name', Object.getType(auto).getName() );
    WriteLineToStringBuilder(sb, 'Max Speed', auto.get_maxSpeed() );
    // proof there that there is no enforcement of the
    // encaspulation we built. It would be nice to not have access
    // to auto._maxSpeed, but as you can see; we do have access.
    WriteLineToStringBuilder(sb, 'Max Speed (no encapsulation)', auto._maxSpeed );
    WriteLineToStringBuilder(sb, 'Most Popular Color', auto.get_color() );
    WriteLineToStringBuilder(sb, 'Wheel Count', auto.get_wheelCount() );
    sb.append(' </div>');
    WriteResults( sb.toString() );
}

function WriteLineToStringBuilder(sb, label, value) {
    sb.append("<b>");
    sb.append( label );
    sb.append(" : </b>");
    sb.append( value );
    sb.append(" <br /> ");
}

function ClearSpeed() {
    WriteResults('');
}

function WriteResults(value) {
    var speed = $get('speedResults');
    speed.innerHTML = value;
}









